package com.crud.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.crud.bean.Department;
import com.crud.bean.Employee;

@Repository
public class EmployeeDao {

	@Autowired
	private DepartmentDao departmentDao;
	
	private static Map<Integer, Employee> employees = new HashMap<>();
	
	private static Integer id = 1007;
	
	static {
		employees.put(1001, new Employee(1001, "AA", "aa@163.com", 1, new Department(101, "D-AA")));
		employees.put(1002, new Employee(1002, "BB", "bb@163.com", 1, new Department(102, "D-BB")));
		employees.put(1003, new Employee(1003, "CC", "cc@163.com", 0, new Department(103, "D-CC")));
		employees.put(1004, new Employee(1004, "DD", "dd@163.com", 1, new Department(104, "D-DD")));
		employees.put(1005, new Employee(1005, "EE", "ee@163.com", 0, new Department(105, "D-EE")));
		employees.put(1006, new Employee(1006, "FF", "ff@163.com", 0, new Department(106, "D-FF")));
		
	}
	
	public Collection<Employee> getAllEmployee(){
		return employees.values();
	}
	
	public void save(Employee employee) {
		if (employee.getId() == null) {
			employee.setId(id++);
		}
		employee.setDepartment(departmentDao.getById(employee.getDepartment().getId()));
		employees.put(employee.getId(), employee);
	}
	
	public void delete(Integer id) {
		employees.remove(id);
	}
	
	public Employee getById(Integer id) {
		return employees.get(id);
	}
	
}
